﻿@using Syncfusion.Blazor.Navigations;

@namespace BlazorDemos.Shared

@inject NavigationManager UriHelper;
@inject SampleService SampleService;

<CascadingValue Value="@this">
    <div class="@rootClass" id='right-pane'>
        <div class='sb-desktop-wrapper'>
            <BreadCrumbComponent @ref="BreadCrumbRef"></BreadCrumbComponent>
            <SampleDescription @ref="SampleDescriptionRef" Visible="true">
                @sampleDescription
            </SampleDescription>
            <div id="sb-content" class='sb-content-section'>
                <SampleNavigator @ref="SampleNavigatorRef" DeviceMode="false"></SampleNavigator>
                <div class='sb-toolbar-splitter sb-custom-item'></div>
                <!-- LoadOn Value Changed due to Source Tab header text value not updated properly issue -->
                <SfTab CssClass='sb-content-tab' id='content-tab' LoadOn="ContentLoad.Dynamic" @bind-SelectedItem="tabSelectedItem">
                    <TabEvents Selecting="OnSourceTabSelecting" Selected="OnSourceTabSelected"></TabEvents>
                    <TabItems>
                        <TabItem>
                            <ChildContent>
                                <TabHeader Text="DEMO" IconCss="sb-icons sb-icon-demo"></TabHeader>
                            </ChildContent>
                            <ContentTemplate>
                                <div>
                                    <div class='sb-demo-section'>
                                        <div class="control-fluid">
                                            <div class="container-fluid">
                                                <div id="control-content">
                                                    @ChildContent
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </ContentTemplate>
                        </TabItem>
                        <TabItem>
                            <ChildContent>
                                <TabHeader Text="SOURCE" IconCss="sb-icons sb-icon-code"></TabHeader>
                            </ChildContent>
                            <ContentTemplate>
                                <div>
                                    <SourceCodeComponent @ref="SourceCodeRef"></SourceCodeComponent>
                                </div>
                            </ContentTemplate>
                        </TabItem>
                    </TabItems>
                </SfTab>
            </div>
            <ActionDescription @ref="ActionDescriptionRef" Visible="true">
                @actionDescription
            </ActionDescription>
            <AdStripComponent />
            <FooterComponent @ref="FooterRef" />
        </div>
    </div>
</CascadingValue>

@code {
    private int tabSelectedItem { get; set; }
    private RenderFragment actionDescription { get; set; }
    private RenderFragment sampleDescription { get; set; }
    private string rootClass = "sb-right-pane e-view " + SampleUtils.RIGHT_PANE;

    protected SourceCodeComponent SourceCodeRef { get; set; }
    protected ActionDescription ActionDescriptionRef { get; set; }
    protected SampleDescription SampleDescriptionRef { get; set; }

    [CascadingParameter]
    protected MainLayout Layout { get; set; }

    /// <summary>
    /// Footer reference for outside usage.
    /// </summary>
    public FooterComponent FooterRef { get; set; }

    /// <summary>
    /// BreadCrumb reference for outside usage.
    /// </summary>
    public BreadCrumbComponent BreadCrumbRef { get; set; }

    /// <summary>
    /// SampleNavigator reference for outside usage.
    /// </summary>
    public SampleNavigator SampleNavigatorRef { get; set; }

    /// <summary>
    /// Child content of RightPane component.
    /// </summary>
    [Parameter]
    public RenderFragment ChildContent { get; set; }

    /// <summary>
    /// Refresh action description for current sample.
    /// </summary>
    public void RefreshActionDescription(RenderFragment description)
    {
        actionDescription = description;
        ActionDescriptionRef.Refresh();
    }

    /// <summary>
    /// Refresh sample description for current sample.
    /// </summary>
    public void RefreshSampleDescription(RenderFragment description)
    {
        SampleService.Update(UriHelper);
        var componentName = SampleService.SampleInfo.Directory == "Buttons/Button" ? "Button" : SampleService.ComponentName;
        BreadCrumbRef.Refresh(componentName, SampleService.SampleInfo.Category, SampleService.SampleInfo.Name);
        sampleDescription = description;
        SampleDescriptionRef.Refresh();
    }

    /// <summary>
    /// Re-render the source code tab with first indexed tab item.
    /// </summary>
    public void TabSelectedItemChange()
    {
        this.tabSelectedItem = 0;
        StateHasChanged();
    }

    // Source tab selecting handler.
    private void OnSourceTabSelecting(Syncfusion.Blazor.Navigations.SelectingEventArgs args)
    {
        if (args.IsSwiped)
        {
            args.Cancel = true;
        }
    }

    // Source tab selected handler.
    private async Task OnSourceTabSelected(Syncfusion.Blazor.Navigations.SelectEventArgs args)
    {
        if (this.SourceCodeRef != null)
        {
            await this.SourceCodeRef.OnSourceTabSelected(args);
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        // Expand or collapse right pane based on the condition.
        if (this.Layout.LeftPaneRef == null || this.Layout.LeftPaneRef.IsExpanded())
        {
            this.rootClass = SampleUtils.RemoveClass(rootClass, SampleUtils.RIGHT_PANE_EXPAND);
            this.rootClass = SampleUtils.AddClass(rootClass, SampleUtils.RIGHT_PANE_COLLAPSE);
        }
        else
        {
            this.rootClass = SampleUtils.RemoveClass(rootClass, SampleUtils.RIGHT_PANE_COLLAPSE);
            this.rootClass = SampleUtils.AddClass(rootClass, SampleUtils.RIGHT_PANE_EXPAND);
        }
    }
}
